$(document).ready(function () {
    layui.use(['laypage','jquery', 'layer', 'table', 'form', 'util'], function(){
        let form = layui.form;
        //初始化日期
        initDate();
        let requestUrl = serverPre + "/manager/scs/v2/signLog/list";
        submit(requestUrl);
    });

    //查询数据
    $('#submitBtn').click(function () {
        let requestUrl = serverPre + "/manager/scs/v2/signLog/list";
        submit(requestUrl);
    });

    //清空条件
    $('#resetBtn').click(function () {
        document.getElementById("queryForm").reset();
    });
});

/**
 * 初始化日期控件
 */
function initDate() {
    layui.use('laydate',function() {
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#startTime',
            type: 'datetime'
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#endTime', //指定元素
            type: 'datetime'
        });
    });
}


/**
 * 分页查询方法
 * @param url
 * @param pageNo
 * @param pageSize
 */
function submit(url, pageNo, pageSize) {
    if(pageNo === undefined){
        pageNo = initPageNo;
    }
    if(pageSize === undefined){
        pageSize = initPageSize;
    }

    layui.use(['table', 'laypage','form'], function () {
        let table = layui.table;
        let laypage = layui.laypage;
        let loading = layer.msg('正在查询...', {icon: 16, shade: 0.3, time:0});

        let requestSN = $('#requestSN').val();
        let templateNo = $('#templateNo').val();
        let templateName = $('#templateName').val();
        let signType = $('#signType').val();
        let signStatus = $('#signStatus').val();
        let needSign = $('#needSign').val();
        let tenantName = $('#tenantName').val();
        let channelName = $('#channelName').val();
        let channelAdmin = $('#channelAdmin').val();
        let startTime = $('#startTime').val();
        let endTime = $('#endTime').val();

        //第一个实例
        table.render({
            elem: '#searchTable'
            ,url: url //数据接口
            ,page: false //开启分页
            ,toolbar: '#tableToolbar'
            , method: 'POST'
            , headers: {"token": getCookie("token")}
            , contentType : "application/json"
            , where: {
                requestSN:requestSN,
                templateNo:templateNo,
                templateName:templateName,
                signType:signType,
                signStatus:signStatus,
                needSign:needSign,
                tenantName:tenantName,
                channelName:channelName,
                channelAdmin:channelAdmin,
                startTime:startTime,
                endTime:endTime,
                pageNo:pageNo,
                pageSize:pageSize}
            ,cols: [[ //表头
                {field: 'dataId',type: 'checkbox'}
                , {field: 'requestSN', title: '流水号', width: '150', align: 'center'}
                , {field: 'templateNo', title: '模板编码', width: '150', align: 'center'}
                , {field: 'templateName', title: '模板名称', width: '200',align: 'center'}
                , {field: 'sealType', title: '签章类型', width: '100',align: 'center'}
                , {field: 'needSign', title: '是否签章', width: '100',align: 'center'}
                , {field: 'signTime', title: '请求签章时间',width: '150',align: 'center'}
                , {field: 'lastTime', title: '耗时',width: '150',align: 'center'}
                , {field: 'callBackTimes', title: '重调次数',width: '150',align: 'center'}
                , {field: 'signStatus', title: '状态', width: '100',align: 'center'}
                , {field: 'thirdPartyChannel', title: '签章渠道', width: '100',align: 'center'}
                , {field: 'callbackUrl', title: '业务回调地址', width: '150',align: 'center'}
                , {field: 'right', width: '250', title: '操作', align: 'center', toolbar: '#barSearch'} //添加工具条,
            ]]
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.pageInfo.totalCount, //解析数据长度
                    "pageNo": res.data.pageInfo.pageNo, //解析数据长度
                    "pageSize": res.data.pageInfo.pageSize, //解析数据长度
                    "data": res.data.pageInfo.list //解析数据列表
                };
            }
            ,done:function (res) {
                layer.close(loading);
                //自定义分页
                laypage.render({
                    elem: 'tablePage'
                    , curr: res.pageNo
                    , count: res.count
                    , limit: res.pageSize
                    ,layout: ['page', 'limit','count', 'skip']
                    ,limits:['20','30','50','100']
                    ,theme: '#1b2b34'
                    ,jump: function(obj,first){
                        if(!first){
                            submit(url, obj.curr, obj.limit);
                        }
                    }
                });
            }
        });

        //工具条
        table.on('tool(searchTable)', function(obj){
            let data = obj.data;
            if(obj.event === 'preview'){
                let signatureStatus = data.signatureStatus;
                if (signatureStatus === 0 || signatureStatus=== 1) {
                    layer.alert('该状态下的记录无法预览', {
                        icon: 1,
                        skin: 'layer-ext-moon'
                    });
                } else {
                    let id = data.id;
                    let signatureStatus = data.signatureStatus;
                    let contractUri = data.contractUri;
                    xadmin.open("预览记录","./preview.html?id="+id+"&signatureStatus="+signatureStatus+"&contractUri="+contractUri,
                        $(window).width()*0.4,$(window).height()*0.7);
                }
            }
        });

    });
}
